<?php

//获取相册基本信息，用于相册列表
function getSetBasicInfo($setId, $coverSize="m"){
	global $wpdb;
	
	$sqlField = "t1.id set_id, t1.title set_title, t1.description set_desc, t2.id id, t1.photos set_photos_num, t2.secret secret, t2.server server, t2.farm farm";
	$sql = "select ".$sqlField." from `flickr_sets` t1 left join (flickr_photos t2) on (t1.primary = t2.id) where t1.id=".$setId." limit 0,1";
	$queryResult = $wpdb->get_results($sql);
	if(mysql_error() == ""){
		return array(
			"queryResult" => $queryResult[0],
			"primarySrc" => getFlickrImgUrl($queryResult[0], $coverSize)
		);
	}else{
		echo mysql_error();
		return null;
	}
}

//获取相册完整信息
function getSetInfo($setId){
	global $wpdb;
	$sql = "select * from flickr_sets where id=".$setId;
	
	$set = $wpdb->get_results($sql);
	if(mysql_error() == ""){
		return $set[0];		
	}else{
		echo mysql_error();
		return false;
	}		
}

/** url 转 ip **/
function urlFilter($farm){
	$ip = "";
	switch ($farm) {
		case '9':
			// $ip = '77.238.160.184';
			return "http://photocache922.flickr.bf1.yahoo.com/";
			break;
		case '8':
			$ip = '98.137.200.139';
			break;
		case '6':
			$ip = '98.139.197.254';
			break;
	}
	
	if(strlen($ip) === 0){
		return "http://farm".$farm.".staticflickr.com/";
	}else{
		return "http://".$ip."/";
	}
}

//拼接 flickr 图片 URL
function getFlickrImgUrl($info, $size="m"){
	if($size=="o"){
		$url = urlFilter($info->farm).$info->server."/".$info->id."_".$info->secret_o."_".$size.".jpg";
	}else{
		$url = urlFilter($info->farm).$info->server."/".$info->id."_".$info->secret."_".$size.".jpg";
	}
	return $url;
}
function pageLog($msg, $more=false){
	echo "<pre>";
	if($more){
		var_dump($msg);
	}else{
		print_r($msg);
	}
	echo "</pre>";
}

// 从相册中捞取图片
function getPhotosBySetId($setId, $imgSize="m"){
	global $wpdb;
	$sql = "select * from flickr_photos where set_id=".$setId." order by date_taken ";
	
	$photos = $wpdb->get_results($sql);
	if(mysql_error() == ""){
		return $photos; 		
	}else{
		echo mysql_error();
		return false;
	}
}


function callFlickrAPI($opt){
	$params = array(
		'api_key'   => '960d3c7371e60b61513b2236939941fb',
		'method'    => null,
		'user_id'   => '75359579@N07',
		'format'    => 'php_serial'
	);
	
	$params = array_merge($params,$opt);


	$encoded_params = array();		
	foreach ($params as $k => $v){
		$encoded_params[] = urlencode($k).'='.urlencode($v);
	}
	
	$url = "http://api.flickr.com/services/rest/?".implode('&', $encoded_params);
	$rsp = file_get_contents($url);
	$rsp_obj = unserialize($rsp);
	
	if ($rsp_obj['stat'] == 'ok'){
		return $rsp_obj;
	}else{
		return false;
	}
}

function getExif($photo_id){
	$APIResult = callFlickrAPI(array(
		'method' => 'flickr.photos.getExif',
		'photo_id' => $photo_id
	));
	$exif = array();
	
	$APIResult = $APIResult['photo']['exif'];
	foreach($APIResult as $eachInfo){
		if($eachInfo['tagspace'] == 'ExifIFD'){
			switch ($eachInfo['tag']) {
				
				//曝光时间
				case 'ExposureTime':
					$exif[] = 'Exposure Time : ' . $eachInfo['raw']['_content'];
					break;
					
				//光圈
				case 'FNumber':
					$exif[] = 'Aperture : ' . $eachInfo['clean']['_content'];
					break;
					
				//曝光程序 
				case 'ExposureProgram':
					$exif[] = 'Exposure Program : ' . $eachInfo['raw']['_content'];
					break;

				//曝光方式 
				case 'ExposureMode':
					$exif[] = 'Exposure Mode : ' . $eachInfo['raw']['_content'];
					break;
					
				//ISO 
				case 'ISO':
					$exif[] = 'ISO : ' . $eachInfo['raw']['_content'];
					break;

				//拍照时间 
				case 'CreateDate':
					$exif[] = 'CreateDate : ' . $eachInfo['raw']['_content'];
					break;

				//曝光补偿 
				case 'ExposureCompensation':
					$exif[] = 'EV : ' . $eachInfo['raw']['_content'];
					break;

				//测光模式 
				case 'MeteringMode':
					$exif[] = 'Metering Mode : ' . $eachInfo['raw']['_content'];
					break;

				//焦距 
				case 'FocalLength':
					$exif[] = 'Focal Length : ' . $eachInfo['raw']['_content'];
					break;

				//白平衡 
				case 'WhiteBalance':
					$exif[] = 'White Balance : ' . $eachInfo['raw']['_content'];
					break;

				//闪光灯 
				case 'Flash':
					$exif[] = 'Flash : ' . $eachInfo['raw']['_content'];
					break;
					
			}
		}
	}
	
	return json_encode($exif);
}

function getExifByAjax($args){
	echo getExif($args['id']);
}

?>